const express = require('express');
const userService = require('../services/user.services');
const asyncHandler = require('../utils/async-handler');

class UserController {
    constructor() {
        this.router = express.Router();
        this.initializeRoutes(); //初始化路由,调用函数
    }

    initializeRoutes() {
        // 获取所有用户
        this.router.get('/', asyncHandler(this.getAllUsers.bind(this)));
        
        // 获取单个用户
        this.router.get('/:id', asyncHandler(this.getUserById.bind(this)));
    }

    async getAllUsers(req, res) {
        const users = await userService.getAllUsers();
        res.json({
            success: true,
            data: users,
            message: '获取用户列表成功'
        });
    }

    async getUserById(req, res) {
        const { id } = req.params;
        const user = await userService.getUserById(id);
        res.json({
            success: true,
            data: user,
            message: '获取用户信息成功'
        });
    }

    getRouter() {
        return this.router;
    }
}

module.exports = new UserController().getRouter();

